﻿<?php
class IndexController extends Zend_Controller_Action
{
   public function indexAction()
   {
      
   }
   
    public function menuAction()
    {

		$categoryModel = new Model_Category();
		$categoryRootArray = $categoryModel->getParentCategory();
		$mainMenu = array();
		$i=0;
		foreach($categoryRootArray as $x)
		{
			$mainMenu[$i]['title'] = $x['cat_ten'];
			$mainMenu[$i]['url']= $x['cat_id'];
			$i++;
		}
		$categoryArray = $categoryModel->getAllCategory();
		$j = 0;
		$category = array();
		foreach($categoryArray as $x)
		{
			$category[$j]['cat_id'] = $x['cat_id'];
			$category[$j]['cat_ten']= $x['cat_ten'];
			$category[$j]['cat_mota'] = $x['cat_mota'];
			$category[$j]['cat_parentID']= $x['cat_parentID'];
			$category[$j]['orderby'] = $x['orderby'];
			$j++;
		}
		$this->view->category = $category;
        $this->view->menu = $mainMenu;
        $this->_helper->viewRenderer->setResponseSegment('menu');
    }
   
   public function menuvertAction()
   {
		/*
		$menuvert = array(
		
		);
		$this->view->menuvert = $menuvert;
		*/
   }
   
   public function searchAction()
   {	
		$form = new Zend_Form();
		$form->setMethod('post');
		
		$form->addElement('text','searchtext');
		$searchtextElement = $form->getElement('searchtext');
		$searchtextElement -> setLabel('Search');
		$searchtextElement->setDecorators(array(
               'ViewHelper',
               'Description',
               'Errors',
               array(array('data'=>'HtmlTag'), array('tag' => 'td')),
               array('Label', array('tag' => 'td')),
               array(array('row'=>'HtmlTag'),array('tag'=>'tr', 'openOnly'=>true))
		));
		
		
		$searchButton = new Zend_Form_Element_Image('searchbutton');
		$searchButton->setImage('/public/img/menu/search2.jpg')
            ->setAttribs(array('id' => 'searchButton',
                                       'type' => 'submit',
                                       'alt' => 'Search!'));      
        $searchButton->setDecorators(array(
               'ViewHelper',
               'Description',
               'Errors',
                array(array('data'=>'HtmlTag'), array('tag' => 'td')),
                array('Label', array('tag' => 'td')),
        ));
		
		$form->addElement($searchButton);
        $form->setDecorators(array(
               'FormElements',
               array(array('data'=>'HtmlTag'),array('tag'=>'table')),
               'Form'

        ));
		$this->view->searchform =$form;
   }
   
   // Hiển thị sản phẩm ở trang home
   public function productAction()
   {
        $productFinder = new Model_Product();
        $this->view->products = $productFinder->fetchLatest();
		

   }
   public function viewbycategoryAction()
   {   
       
       $id = (int)$this->getRequest()->getParam('cat_id');
       $productFind= new Model_Product();
	   $products=$productFind->getProductCategory($id);
	   
	   if(count($products)){
	   
		   /* $pageNumber = 3 ; //or whatever you wish
			$itemNumber = 4; //or whatever you wish
			$paginator = Zend_Paginator::factory($products);
			
			$paginator->setItemCountPerPage($pageNumber);
			$paginator->getItemsByPage($itemNumber);
			$paginator->setCurrentPageNumber($this->_getParam('page'));
			Zend_Paginator::setDefaultScrollingStyle('Sliding');
			$view = new Zend_View();
			$paginator->setView($view);*/

			//this are your results from db        
			$this->view->products = $products;
		}
		else {
		
		  $catfind=new Model_Category();
		  $catresult=$catfind->getChildCategory($id);
		  $this->view->catchild=$catresult;
		
		
		}

		//this is paginator
		//$this->view->paginator = $paginator;
   }
 
	public function yahooAction()
	{
	
	}
	
	//lay ra product theo categoryID
	public function displayproductbyCategoryAction()
	{
	$categoryModel = new Model_Category();
	$categoryRootArray = $categoryModel->getParentCategory();
	
	
	}
	
	
}